from libavg import avg
from config import Config
from points import PointsBar

class ScoreScreen(avg.DivNode):
    
    __background = None
        
    def __init__(self, parent, pointBarP1, pointBarP2):
        # init parent divnode
        avg.DivNode.__init__(self, size=(Config.resX, Config.resY), parent=parent)
    
        #init background
        self.__background = avg.ImageNode(pos = (0,0),
                    size = (Config.resX, Config.resY),
                    href = "../data/img/score_bg_720.png", parent=self)
        
        # fetch player information
        P1Name = str(pointBarP1.getName())
        P1Points = pointBarP1.getPoints()
        P1Condition = pointBarP1.getCondition()
        
        P2Name = str(pointBarP2.getName())
        P2Points = pointBarP2.getPoints()
        P2Condition = pointBarP2.getCondition()
        
        
        winnerName = ""
        winnerPoints = 0
        
        if(P1Points == P2Points):
            winnerName = "BEIDE"
            
        elif (P1Points > P2Points):
            winnerName = "RED"
            winnerPoints = P1Points
        else:
            winnerName = "BLUE"
            winnerPoints = P2Points
        
        
        #
        # create winner panel
        #
        winnerPanel = avg.DivNode()
        winnerPanel.pos = (Config.resX / 2, Config.resY / 4)
        
        # create winner panel components
        winnerNamePanel = avg.WordsNode(pos = (0, -21) , size = (Config.resX, Config.ctrlButtonHeight), parent = winnerPanel, text=str(winnerName), font="Arial", alignment = "center", color="FFFFFF", fontsize=75, variant="bold", hint=True)
        winnerPointPanel = avg.WordsNode(pos = (9, 82), parent = winnerPanel, text=str(winnerPoints), font="Arial", color="FFFFFF", fontsize=50, variant="bold", hint=True)

        
        #
        # create result panel
        #
        resultPanel = avg.DivNode()
        resultPanel.pos = (Config.resX/2 + Config.scoreElementWidth, 23* (Config.resY / 32))
        resultPanel.width = Config.resX 
        resultPanel.height = Config.resY 
        
   
        
        bluePointPanel = avg.WordsNode(pos = (100,2), text=str(P1Points), font="Arial", color="FFFFFF", variant="bold", alignment = "right", fontsize=50, hint=True)
        redPointPanel = avg.WordsNode(pos = (100,105), text=str(P2Points), font="Arial", color="FFFFFF", variant="bold", alignment = "right", fontsize=50, hint=True)
        
        # add components to parent panel
        resultPanel.appendChild(bluePointPanel)
        resultPanel.appendChild(redPointPanel)
        
        # add result panel
        self.appendChild(winnerPanel)
        self.appendChild(resultPanel)
        